var form = layui.form,
    table = layui.table,
    $ = layui.jquery,
    id = getQueryVariable("id"),
    tabElement = layui.element;


$('#editor-form').find('input,textarea,select').attr('disabled', true);


let executorList= [];

let productList = [];
let fileList = [];
let colourList = getdictionary(6, 41);//颜色
let unitList = getdictionary(6, 42);//单位

let blueprintContentList=[];//目录明细
//临时id
let tempId='';
//明细临时id，用于保存附件
let detailIdTemp='';
//明细附件
let contentUploadFileList=[];




layer.ready(function () {
    $.ajax({
        async: false,
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:id,
        url: 'blueprint/selectByKey',
        success: function (da) {
            if (da.status == 200) {
                form.val("editor-form", {
                    "number": da.data.number,
                    "createTime": da.data.createTime,
                    "operatorName": da.data.operatorName,
                    "remark": da.data.remark,
                })

                blueprintContentList=da.data.blueprintContentList;
                for(let j in blueprintContentList){
                    blueprintContentList[j]['tempId'] = blueprintContentList[j]["id"];
                }
                fileList = queryFileByBid(id);
            } else {
                layer.msg("初始化失败!");
            }
        },
        error: function (data) {
            layer.msg("初始化失败!");
        }
    });
})


//---附件
table.render({
    elem: '#attachment'
    , data: fileList
    , toolbar: '#attachmenttoolbar'
    , cols: [[
        {field: 'wjName', title: '文件名称', fixed: true,style:"overflow:hidden", event:'fileName'},
        // {field: 'fileName', title: '文件名称', fixed: true,style:"overflow:hidden"},
        {field: 'fileUrl', title: '文件路径', fixed: true,style:"overflow:hidden"},
        {field: 'fileType', title: '文件类型', fixed: true},
        {field: 'fileSize', title: '文件大小(KB)', fixed: true},
        {field: 'fileExecutor', title: '操作员', fixed: true,
            templet: function(data) {
                let name = "";
                $.each(executorList, function(index, item) {
                    if (data.fileExecutor==item.code){
                        name = item.name;
                        return ;
                    }
                });
                return name;
            }
        },
        {field: 'fileUploadTime', title: '上传时间', fixed: true,
            templet: function(d) {
                return layui.util.toDateString(d.createTime,"yyyy-MM-dd");
            }
        },
        {fixed: 'right', width: 180, align: 'center', toolbar: '#attachmenteditor', title: '操作'},
    ]]
    , id: 'attachmenttableid'
    , height: 380
    , page: true
});

table.on('tool(attachmenttable)', function (obj) {
    let data = obj.data;
    if (obj.event === 'download') { //下载
        downFileLoad(data.fileUrl+'/'+data.fileName);
    }else if (obj.event === 'fileName'){
        onLineFile(data.fileUrl+'/'+data.fileName);
    }
});

//---操作日志
table.render({
    elem: '#otherwarehouselog'
    , data: null
    , cols: [[
        {field: 'operationtype', title: '操作类型', fixed: true}
        , {field: 'theoperator', title: '操作员'}
        , {field: 'operationdate', title: '操作日期'}
        , {field: 'modifythecontent', title: '修改内容'}
    ]]
    , id: 'logtableid'
    , height: 380
    , limit: 30//显示的数量
    , page: true
});

$("#shutdown").on("click", function () {
    //关闭此页面
    parent.detab("views/jsp/other/tpl/viewdrawings.jsp" + (id == '' ? '' : '?id=' + id));
});


//--目录信息
table.render({
    elem: '#detailInfo',
    id: 'detailInfoId',
    data: blueprintContentList,
    toolbar: '#detailInfoToolbar',
    cols: [[
        {checkbox: true,title:'选择', width: 50},
        {field: 'id', hide:true},
        {field: 'contentName', title: '产品目录', width: 1000,event:'content'},
        {field: 'contentCode', hide:true},
        {field: 'contentPath', hide:true},
        {field: 'contentPid', hide:true},
        {field: 'contentId', hide:true},
        {field: 'supplierName', title: '供应商名称', width: 1000},
        {field: 'supplierCode', hide:true},
        {fixed: 'right', title: '操作', width: 80, align: 'center'},
    ]],
    page: true,
    limit:50,
    height: 380,
    done: function (res, curr, count) {
        $(".layui-table select").parent(".layui-table-cell").css('overflow','visible')
        $(".layui-form-select").parents(".layui-table-cell").css('overflow','visible')
    }
});


//切换选项卡时隐藏下面的表格
tabElement.on('tab(tabElement)', function(){
    let layId=this.getAttribute('lay-id');
    if (layId!=1){
        detailIdTemp='';
        $("#hidden").hide();
    }
});

table.on('tool(detailInfoTable)', function (obj) {
    let data = obj.data;
    if (obj.event === 'content') {
        $("#hidden").show();
        $.ajax({
            type: 'get',
            url: "blueprint/selectBlueprintContentList?blueprintId="+id+"&detailId="+data.id,
            async: false,
            success: function (da) {
                if (da.status == 200) {
                    contentUploadFileList = da.data ;
                }
            },
            error: function (data) {
                // layer.msg("获取失败!");
            }
        });

        table.render({
            elem: '#contentUpload'
            , data: contentUploadFileList
            , cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', hide:true},
                {field: 'blueprintId', hide:true},
                {field: 'detailId', hide:true},
                {field: 'fileOriginalName', title: '文件名称',style:"overflow:hidden", event:'fileName'},
                // {field: 'fileUrl', title: '文件路径',style:"overflow:hidden"},
                {field: 'fileType', title: '文件类型'},
                {field: 'fileSize', title: '文件大小(KB)'},
                {field: 'operationName', title: '操作员'},
                {field: 'operationCode', hide:true},
                {field: 'fileUploadTime', title: '上传时间',
                    templet: function(d) {
                        return layui.util.toDateString(d.createTime,"yyyy-MM-dd");
                    }
                },
                {fixed: 'right', width: 150, align: 'center', toolbar: '#attachmenteditor', title: '操作'},
            ]]
            , id: 'contentUploadId'
            , page: false
            , height: 300
        });

    }
});

table.on('tool(contentUploadTable)', function (obj) {
    let data = obj.data;
    if (obj.event === 'download') { //下载
        downFileLoad(data.fileUrl+'/'+data.fileName);
    }else if (obj.event === 'fileName'){
        onLineFile(data.fileUrl+'/'+data.fileName);
    }
});

//产品
function queryProductByBid(bId) {
    let resList = [];
    $.ajax({
        type: 'POST',
        url: 'blueprint/selectProductByBid/' + bId,
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        async: false,
        success: function (res) {
            if (res.status == 200) {
                if (res.data != null) {
                    resList = res.data;
                    form.val("editor-form", {
                        "productNo": res.data.productNo,
                        "specNo": res.data.specNo,
                        "normExplain": res.data.normExplain,
                        "chineseName": res.data.chineseName,
                        "englishName": res.data.englishName,
                        "chineseDesc": res.data.chineseDesc,
                        "englishDesc": res.data.englishDesc,
                        "unit": res.data.unit,
                        "colour": res.data.colour,
                        "customerArticleNo": res.data.customerArticleNo,
                    })
                    // $("#colourSelect").val(res.data.colour);
                    // $("#unitSelect").val(res.data.unit);
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 3});
                }
            } else {
                // layer.msg("产品初始化失败!");
            }
        },
        error: function (data) {
            layer.msg("产品初始化失败!");
        }
    });
    return resList;
}

//附件
function queryFileByBid(bId) {
    let resList = [];
    $.ajax({
        type: 'POST',
        url: 'blueprint/selectFileByBid/' + bId,
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        async: false,
        success: function (res) {
            if (res.status == 200) {
                if (res.data != null) {
                    resList = res.data;
                    form.val("editor-form", {
                        "wjName": res.data.wjName,
                        // "fileName": res.data.fileName,
                        "fileUrl": res.data.fileUrl,
                        "fileType": res.data.fileType,
                        "fileSize": res.data.fileSize,
                        "fileExecutor": res.data.fileExecutor,
                        "fileUploadTime": res.data.fileUploadTime,
                    })
                } else {
                    layer.msg(res.msg, {icon: 3});
                }
            } else {
                // layer.msg("附件初始化失败!");
            }
        },
        error: function (data) {
            layer.msg("附件初始化失败!");
        }
    });
    return resList;
}


function update(data) {
    var tf = false;
    data.field.id = id;
    $.ajax({
        type: 'POST',
        data: JSON.stringify(data.field),
        url: 'blueprint/update',
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        async: false,
        success: function (da) {
            if (da.status == 200) {
                tf = true;
            }else{
                layer.msg("修改失败!");
            }
        },
        error: function (data) {
            layer.msg("修改失败!");
        }
    });
    return tf;
}